package com.vmware.elastic.high;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

/**
 * @apiNote 全量查询
 *
 * 响应时间:0s
 * {
 *   "_index" : "vmware",
 *   "_type" : "_doc",
 *   "_id" : "1005",
 *   "_score" : 1.0,
 *   "_source" : {
 *     "name" : "王五",
 *     "age" : 30
 *   }
 * }
 * {
 *   "_index" : "vmware",
 *   "_type" : "_doc",
 *   "_id" : "1006",
 *   "_score" : 1.0,
 *   "_source" : {
 *     "name" : "赵六",
 *     "age" : 40
 *   }
 * }
 * {
 *   "_index" : "vmware",
 *   "_type" : "_doc",
 *   "_id" : "1007",
 *   "_score" : 1.0,
 *   "_source" : {
 *     "name" : "陈⑦",
 *     "age" : 50
 *   }
 * }
 */
public class DocumentMatchAllQuery {
    public static void main(String[] args) throws Exception {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("10.192.193.98", 9200))
        );
        SearchRequest request = new SearchRequest("vmware");
        request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));//构建查询，设置为全量查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();//获取查询结果
        TimeValue took = response.getTook(); //获取响应时间
        System.out.println("响应时间:" + took);
        for (SearchHit hit : hits) {
            System.out.println(hit);
        }
        client.close();
    }
}
